/*
 * Copyright 2011 Kim Lindhardt Madsen
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package dk.lindhardt.gwt.geie.shared;

import com.google.gwt.user.client.ui.Widget;
import dk.lindhardt.gwt.geie.client.GeieAnchor;

import java.io.Serializable;

/**
 * Describes a cell in a {@link TableLayout}
 *
 * @param <T> the type of the values in this cell
 */
abstract class AbstractCell<T extends Serializable> implements Serializable, Cell<T> {

   private static final long serialVersionUID = 3427698939148132293L;

   protected T value;
   protected Position position;
   protected CellFormatKey cellFormatKey;
   private Link link;
   private boolean locked;
   private boolean dirty;

   /**
    * For serialization
    */
   AbstractCell() {
   }

   /**
    * New instance of a tableCell
    * @param value          the value to be presented in cell
    * @param position       the position of this cell
    * @param cellFormatKey key of the table format for this cell
    */
   public AbstractCell(T value, Position position, CellFormatKey cellFormatKey) {
      this.value = value;
      this.position = position;
      this.cellFormatKey = cellFormatKey;
   }

   /**
    * New instance of a tableCell
    * @param value            the value to be presented in cell
    * @param row              the row position of this cell
    * @param col              the column position of this cell
    * @param cellFormatKey   key of the table format for this cell
    */
   public AbstractCell(T value, int row, int col, CellFormatKey cellFormatKey) {
      this.value = value;
      this.position = new Position(row, col);
      this.cellFormatKey = cellFormatKey;
   }

   /**
    * New instance of a tableCell
    * @param value            the value to be presented in cell
    * @param row              the row position of this cell
    * @param col              the column position of this cell
    * @param spanRow          the number of rows to span
    * @param spanCol          the number of column to span
    * @param cellFormatKey   key of the table format for this cell
    */
   public AbstractCell(T value, int row, int col, int spanRow, int spanCol, CellFormatKey cellFormatKey) {
      this.value = value;
      this.position = new Position(row, col, spanRow, spanCol);
      this.cellFormatKey = cellFormatKey;
   }

   public T getValue() {
      return value;
   }

   public void setValue(T value) {
      this.value = value;
   }

   public Position getPosition() {
      return position;
   }

   public void setPosition(Position position) {
      this.position = position;
   }

   public CellFormatKey getCellFormatKey() {
      return cellFormatKey;
   }

   public void setCellFormatKey(CellFormatKey cellFormatKey) {
      this.cellFormatKey = cellFormatKey;
   }

   public Link getLink() {
      return link;
   }

   public void setLink(Link link) {
      this.link = link;
   }

   public boolean isLocked() {
      return locked;
   }

   public void setLocked(boolean locked) {
      this.locked = locked;
   }

   public boolean isDirty() {
      return dirty;
   }

   public void setDirty(boolean dirty) {
      this.dirty = dirty;
   }

   /**
    * If this cell is linking it is wrapped in an anchor
    * @param widget the widget
    * @return widget
    */
   protected Widget wrapIfLink(Widget widget) {
      if (link != null) {
         return new GeieAnchor(widget, link);
      }
      return widget;
   }

   @Override
   public String toString() {
      return "Cell{" + position + " value=" + value + " cellFormat=" + cellFormatKey.getKey() + " link=" + link + "}";
   }

   @Override
   public boolean equals(Object o) {
      if (this == o) {
         return true;
      }
      if (o == null || getClass() != o.getClass()) {
         return false;
      }

      AbstractCell that = (AbstractCell) o;

      if (cellFormatKey != null ? !cellFormatKey.equals(that.cellFormatKey) : that.cellFormatKey != null) {
         return false;
      }
      if (link != null ? !link.equals(that.link) : that.link != null) {
         return false;
      }
      if (position != null ? !position.equals(that.position) : that.position != null) {
         return false;
      }
      if (value != null ? !value.equals(that.value) : that.value != null) {
         return false;
      }

      return true;
   }

   @Override
   public int hashCode() {
      int result = value != null ? value.hashCode() : 0;
      result = 31 * result + (position != null ? position.hashCode() : 0);
      result = 31 * result + (cellFormatKey != null ? cellFormatKey.hashCode() : 0);
      result = 31 * result + (link != null ? link.hashCode() : 0);
      return result;
   }
}
